﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EzDesk.AutoCheckAttendance
{
    public partial class MainForm : Form, IMainView
    {
        private MainPresenter _presenter;
        private bool _isRunning;

        public MainForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            
            _presenter = new MainPresenter(this);
        }

        public void AddLog(string fullLogStr)
        {
            lstLogItems.Items.Add(fullLogStr);
            lstLogItems.SetSelected(lstLogItems.Items.Count - 1, true);
        }


        public void RefreshTimemark(DateTime _timeMark)
        {
            lblLatestUpdate.Text = _timeMark.ToString();
        }

        private void lstLogItems_DrawItem(object sender, DrawItemEventArgs e)
        {
            string s = this.lstLogItems.Items[e.Index].ToString();
            if ((e.State & DrawItemState.Selected) == 0)
            {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
                if (s.Contains("搞定"))
                {
                    e.Graphics.DrawString(s, this.Font, Brushes.Red, e.Bounds);
                }
                else
                    e.Graphics.DrawString(s, this.Font, new SolidBrush(this.ForeColor), e.Bounds);
            }
            else
            {
                e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
                e.Graphics.DrawString(s, this.Font, Brushes.White, e.Bounds);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_isRunning)
            {
                _presenter.StopMonitor();
            }
            else
            {
                _presenter.StartMonitor();
            }
        }

        public void ChangeIsMonitorState(bool isRunning)
        {
            _isRunning = isRunning;
            if (!isRunning)
            {
                lstLogItems.Items.Clear();
            }
            panel1.BackColor = isRunning ? Color.Green : Color.Red;
            button1.Text = isRunning ? "停止监控" : "开始监控";
        }
    }
}
